package cn.edu.dlmu.cogo.framework.context;

import cn.edu.dlmu.cogo.framework.annotation.Component;
import cn.edu.dlmu.cogo.framework.config.JavaConfig;
import cn.hutool.core.date.DateUtil;
import cn.hutool.setting.dialect.Props;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


/**
 * @author hongda.li@hand-china.com 2022/3/14 9:57
 */
@Component
public class Environment {

    /**
     * 属性文件本地缓存
     */
    private final Map<String,Props> propsMap = new HashMap<>();

    public static String getSystemTime(){
        return DateUtil.now();
    }

    public Props getProps(String path) {
        Props props = propsMap.get(path);
        if (props != null) {
            return props;
        }else {
            Props newProp = new Props(path);
            propsMap.put(path, newProp);
            return newProp;
        }
    }

    public Object getProps(String path, String propertyName){
        Props props = propsMap.get(path);
        if (props != null) {
            return props.get(propertyName);
        }else {
            Props newProp = new Props(path);
            propsMap.put(path, newProp);
            return newProp.get(propertyName);
        }
    }

    public Object getBean(Class<?> clazz){
        return ContextContainer.getApplicationContext().getBean(clazz);
    }

    public Object getBean(String beanName){
        return ContextContainer.getApplicationContext().getBean(beanName);
    }

    public JavaConfig getConfig(Class<? extends JavaConfig> clazz){
        return Objects.requireNonNull(JavaConfig.getConfig(clazz));
    }
}
